﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace Hangman
{
    public partial class Default : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                switch (uiMultiView.ActiveViewIndex)
                {
                    case 0:
                        //Welcome view
                        uiMultiView.ActiveViewIndex = 1;
                        hangmanimage.Src = "hangman.gif?status=0";
                        uiKeyboardControl.Init();

                        uiKeyboardControl.DisableKey("A");
                        uiKeyboardControl.Draw();

                        string category = uiCategory.SelectedValue;
                        string result = GetResultString(category);
                        this.Controls.Add(new LiteralControl(":" + result + ":"));

                        break;
                    case 1:
                        //Game is going on
                        hangmanimage.Src = "hangman.gif?status=0";
                        break;
                    case 2:
                        //Winning view
                        break;
                    case 3:
                        //Loosing view
                        break;
                }
            }
            else
            {
                uiMultiView.ActiveViewIndex = 0;
            }
        }
        /*
         * Gets a random string from the XML file, that the user has to guess
         */
        private string GetResultString(string category)
        {
            XmlDocument doc = new XmlDocument();
            string docpath = Server.MapPath("~/Hangman.xml");
            doc.Load(docpath);


            string xPath = "//hangman/categories/" + category;
            XmlNodeList list = doc.SelectNodes(xPath).Item(0).ChildNodes;

            

            int nbr = list.Count;
            Random rnd = new Random();
            int n = rnd.Next(0, nbr);
            string tmp = list.Item(n).InnerText;
            return tmp;
        }

        //user clicked the start button - process the game by swiching the view
        /*protected void StartGame(object sender, EventArgs e)
        {
            uiMultiView.ActiveViewIndex = 1;
            hangmanimage.Src = "hangman.gif?status=0";
            uiKeyboardControl.Draw();
        }*/
        private void HandleGameProcess()
        {
        }

    }
}